<?php

namespace App\Services\UserInviteInfo;


use App\Models\Product;
use Illuminate\Support\Str;
use Intervention\Image\Gd\Font;
use Illuminate\Support\Facades\File;
use Intervention\Image\Facades\Image;
use Illuminate\Support\Facades\Storage;

class MakeUserAvatar
{
	
	protected $name;
	protected $id;
	protected $avatar;
	
	public function __construct($name,$id,$avatar=null)
	{
		$this->name = Str::substr($name, 0, 1);
		$this->id = $id;
		$this->avatar = $avatar;
	}
	
	protected function getPosterName()
	{
		return "avatar-user-{$this->id}.png";
	}

	protected function getPosterNameCircle()
	{
		return "avatar-user-circle-{$this->id}-".rand(1111,9999).".png";
	}
	
	protected function getRemotePosterDir()
	{
		return 'images/wap-user-avatar';
	}
	
	protected function getRemotePosterSavePath()
	{
		return $this->getRemotePosterDir() . '/' . $this->getPosterName();
	}
	
	protected function getLocalPosterSavePath(string $name)
	{
		$localPath = public_path('temp/wap-user-avatar');
		if (! File::isDirectory($localPath)) {
			File::makeDirectory($localPath, 755, true, true);
		}
		return "{$localPath}/{$name}.png";
	}
	
	public function make()
	{
		/*if (Storage::exists($this->getRemotePosterSavePath())) {
			return Storage::url($this->getRemotePosterSavePath());
		}*/
		
		$posterDir = $this->getRemotePosterDir();
		$posterName = !empty($this->avatar) ? $this->getPosterNameCircle() :$this->getPosterName();
/*
		
		
		$back = Image::make(oss('images/wap/zsjd-user-poster-bg.png'));
		$name = Str::limit($this->user->name, 12);
		$inviteCode = $this->user->invite_code ? "邀请码：".$this->user->invite_code : '';
		$qrcode = $this->user->inviteQrcode();
		$qrcode = Image::make($qrcode)->resize(440, 440);

		$avatar = $this->user->avatar ? img_path($this->user->avatar) : oss('images/wap/common_logo.png');
		$avatar = Image::make($avatar)->resize(90, 90);*/

		$backgroundColorArr = ["#FF4758","#2E303F","#00C2C3"];
		$rand = rand(0,2);
		$backgroundColor = $backgroundColorArr[$rand];

		if($this->avatar){

			$img=Image::make($this->avatar)->resize(164,164);

			$new= Image::canvas(164,164);

			$r=$img->width() /2;

			for($x=0;$x<$img->width();$x++) {

				for($y=0;$y<$img->height();$y++) {

					$c=$img->pickColor($x,$y,'array');

					if(((($x-$r) * ($x-$r) + ($y-$r) * ($y-$r)) < ($r*$r))) {

						$new->pixel($c,$x,$y);

					}
				}
			}	

			$new->save($this->getLocalPosterSavePath($posterName));
			

		}else{
			Image::canvas(90, 90)
			->circle(90, 45, 45, function ($draw)use($backgroundColor) {//在图片里面画圆，直径，x坐标，y坐标
	            $draw->background($backgroundColor);//背景色
	            $draw->border(1, '#fff');//圆边框，与背景色
	         })
	        ->fit(90,90)
	        ->trim('transparent', array('top', 'bottom', 'left', 'right'))
			->text($this->name, 45, 45, function (Font $font) {
				$font->file(public_path('font/MSYH.TTF'));
				$font->color('#fff');
				$font->align('center');
           		$font->valign('center');
				$font->size(60);
			})
			->save($this->getLocalPosterSavePath($posterName));

		}
		
		
		// move to oss.
		Storage::putFileAs($posterDir, $this->getLocalPosterSavePath($posterName), $posterName);
		
		// remove local poster.
		File::delete($this->getLocalPosterSavePath($posterName));

		
		return Storage::url("{$posterDir}/{$posterName}");
	}
}
